package com.bike.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhAgent;
import com.bike.entity.HhPayRecord;
import com.bike.mapper.HhPayRecordMapper;
import com.bike.util.Constant;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhPayRecordServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhPayRecordMapper hhPayRecordMapper;
	
	/**
	* 保存
	*/
	public void saveOrUpdate(PageData pd, HhPayRecord record) {
		if(pd.getLong("payRecordId")>0){
			 //编辑
			hhPayRecordMapper.updateByPrimaryKeySelective(record);
		}else{
			hhPayRecordMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhPayRecordById(long payRecordId) {
		return hhPayRecordMapper.deleteByPrimaryKey(payRecordId);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhPayRecordList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		 
		List<HhPayRecord> records= hhPayRecordMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhPayRecord>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhPayRecord> listAll(PageData pd) {
		return  hhPayRecordMapper.selectAllByPd(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhPayRecord selectByHhPayRecordId(Long payRecordId) {
		return hhPayRecordMapper.selectByPrimaryKey(payRecordId);
	}

	public void insertAgentPayRecord(HhAgent agent, BigDecimal amount, int type,String typeStr,boolean inner) {
		HhPayRecord payRecord=new HhPayRecord();
		 payRecord.setAddTime(TimeManager.currentTime());
		 payRecord.setMoney(amount);
		 if(inner){
			 payRecord.setToUserId(agent.getAgentId());
			 payRecord.setToUserName(agent.getName());
		 }else{
			 payRecord.setUserId(agent.getAgentId());
			 payRecord.setUserName(agent.getName());
		 }
		 payRecord.setTransType(typeStr);
		 payRecord.setType(type);
		 hhPayRecordMapper.insertSelective(payRecord);
	}
}

